// page/index/index.js
import request from "../../utils/request";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [],
        recommendList: [],
        leaderBoardList: []
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getBannerList();
        this.getRecommendList();
        this.getLeaderBoardList();
    },
    // 获取轮播图
    async getBannerList() {
        let bannerData = await request("/banner", {type: 1})
        this.setData({
            bannerList: bannerData.banners
        })
    },
    // 跳转至每日推荐
    handlerRecommend() {
        wx.navigateTo({
            url: '/page/recommend/recommend'
        })
    },
    // 获取推荐歌单
    async getRecommendList() {
        let recommendData = await request("/personalized", {limit: 10});
        this.setData({
            recommendList: recommendData.result
        })
    },
    // 获取排行榜歌曲
    async getLeaderBoardList() {
        let resultArr = [];
        for (let i = 0; i < 4; i++) {
            let leaderBoardData = await request("/top/list", {idx: i})
            let topListItem = {name: leaderBoardData.playlist.name, tracks: leaderBoardData.playlist.tracks.slice(0, 3)}
            resultArr.push(topListItem)
            this.setData({
                leaderBoardList: resultArr
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
